package com.wgke.bluetooth.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

public class PressureMatrixView extends View {
    // 矩阵尺寸
    public static final int MATRIX_WIDTH = 100;
    public static final int MATRIX_HEIGHT = 75;

    // 默认参数
    private float cellSize = 10f;    // 每个矩形块的大小(dp)
    private float gapSize = 2f;      // 矩形块之间的间隔(dp)

    // 压力数据
    private float[][] pressureData = new float[MATRIX_HEIGHT][MATRIX_WIDTH];

    // 画笔
    private Paint cellPaint = new Paint();

    // 转换系数
    private float density; // 屏幕密度

    public PressureMatrixView(Context context) {
        super(context);
        init(context);
    }

    public PressureMatrixView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PressureMatrixView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        density = context.getResources().getDisplayMetrics().density;
        cellPaint.setStyle(Paint.Style.FILL);
        generateRandomData();
    }

    // 生成随机数据
    public void generateRandomData() {
        Random random = new Random();
        int centerX = MATRIX_WIDTH / 2;
        int centerY = MATRIX_HEIGHT / 2;

        for (int y = 0; y < MATRIX_HEIGHT; y++) {
            for (int x = 0; x < MATRIX_WIDTH; x++) {
                float distance = (float) Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
                pressureData[y][x] = 100f * (float) Math.exp(-distance / 50f);

                // 添加一些随机变化
                pressureData[y][x] *= 0.8f + 0.4f * random.nextFloat();
                pressureData[y][x] = Math.min(100f, Math.max(0f, pressureData[y][x]));
            }
        }
        invalidate();
    }

    // 设置压力数据
    public void setPressureData(float[][] data) {
        if (data != null && data.length == MATRIX_HEIGHT && data[0].length == MATRIX_WIDTH) {
            pressureData = data;
            invalidate();
        }
    }

    // 设置单元格大小
    public void setCellSize(float sizeInDp) {
        this.cellSize = sizeInDp;
        invalidate();
    }

    // 设置间隔大小
    public void setGapSize(float sizeInDp) {
        this.gapSize = sizeInDp;
        invalidate();
    }

    // 根据压力值获取颜色
    private int getPressureColor(float pressure) {
        if (pressure <= 0) {
            return Color.rgb(230, 230, 230); // 灰白色
        }

        float r, g, b;
        if (pressure < 50) {
            // 浅蓝(0,255,255)到绿(0,255,0)
            float t = pressure / 50f;
            r = 0;
            g = 255;
            b = 255 * (1 - t);
        } else {
            // 绿(0,255,0)到大红(255,0,0)
            float t = (pressure - 50) / 50f;
            r = 255 * t;
            g = 255 * (1 - t);
            b = 0;
        }

        return Color.rgb((int)r, (int)g, (int)b);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 转换为像素值
        float cellSizePx = cellSize * density;
        float gapSizePx = gapSize * density;

        // 计算总尺寸
        float totalWidth = MATRIX_WIDTH * (cellSizePx + gapSizePx) - gapSizePx;
        float totalHeight = MATRIX_HEIGHT * (cellSizePx + gapSizePx) - gapSizePx;

        // 计算绘制起点（居中）
        float startX = (getWidth() - totalWidth) / 2;
        float startY = (getHeight() - totalHeight) / 2;

        // 绘制每个单元格
        for (int y = 0; y < MATRIX_HEIGHT; y++) {
            for (int x = 0; x < MATRIX_WIDTH; x++) {
                float left = startX + x * (cellSizePx + gapSizePx);
                float top = startY + y * (cellSizePx + gapSizePx);

                cellPaint.setColor(getPressureColor(pressureData[y][x]));
                canvas.drawRect(left, top, left + cellSizePx, top + cellSizePx, cellPaint);
            }
        }
    }
}
